package cn.iocoder.yudao.module.tms.controller.admin.companyaccountperiod;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.tms.controller.admin.companyaccountperiod.vo.*;
import cn.iocoder.yudao.module.tms.dal.dataobject.companyaccountperiod.CompanyAccountPeriodDO;
import cn.iocoder.yudao.module.tms.service.companyaccountperiod.CompanyAccountPeriodService;

@Tag(name = "管理后台 - 公司账期信息")
@RestController
@RequestMapping("/tms/company-account-period")
@Validated
public class CompanyAccountPeriodController {

    @Resource
    private CompanyAccountPeriodService companyAccountPeriodService;

    @PostMapping("/create")
    @Operation(summary = "创建公司账期信息")
    @PreAuthorize("@ss.hasPermission('tms:company-account-period:create')")
    public CommonResult<Long> createCompanyAccountPeriod(@Valid @RequestBody CompanyAccountPeriodSaveReqVO createReqVO) {
        return success(companyAccountPeriodService.createCompanyAccountPeriod(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新公司账期信息")
    @PreAuthorize("@ss.hasPermission('tms:company-account-period:update')")
    public CommonResult<Boolean> updateCompanyAccountPeriod(@Valid @RequestBody CompanyAccountPeriodSaveReqVO updateReqVO) {
        companyAccountPeriodService.updateCompanyAccountPeriod(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除公司账期信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tms:company-account-period:delete')")
    public CommonResult<Boolean> deleteCompanyAccountPeriod(@RequestParam("id") Long id) {
        companyAccountPeriodService.deleteCompanyAccountPeriod(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除公司账期信息")
    @PreAuthorize("@ss.hasPermission('tms:company-account-period:delete')")
    public CommonResult<Boolean> deleteCompanyAccountPeriodList(@RequestParam("ids") List<Long> ids) {
        companyAccountPeriodService.deleteCompanyAccountPeriodListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得公司账期信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tms:company-account-period:query')")
    public CommonResult<CompanyAccountPeriodRespVO> getCompanyAccountPeriod(@RequestParam("id") Long id) {
        CompanyAccountPeriodDO companyAccountPeriod = companyAccountPeriodService.getCompanyAccountPeriod(id);
        return success(BeanUtils.toBean(companyAccountPeriod, CompanyAccountPeriodRespVO.class));
    }

    @GetMapping("/get-by-company")
    @Operation(summary = "根据公司ID获得账期信息")
    @Parameter(name = "companyId", description = "公司ID", required = true, example = "2048")
    @PreAuthorize("@ss.hasPermission('tms:company-account-period:query')")
    public CommonResult<CompanyAccountPeriodRespVO> getCompanyAccountPeriodByCompanyId(@RequestParam("companyId") Long companyId) {
        CompanyAccountPeriodDO companyAccountPeriod = companyAccountPeriodService.getCompanyAccountPeriodByCompanyId(companyId);
        return success(BeanUtils.toBean(companyAccountPeriod, CompanyAccountPeriodRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得公司账期信息分页")
    @PreAuthorize("@ss.hasPermission('tms:company-account-period:query')")
    public CommonResult<PageResult<CompanyAccountPeriodRespVO>> getCompanyAccountPeriodPage(@Valid CompanyAccountPeriodPageReqVO pageReqVO) {
        PageResult<CompanyAccountPeriodDO> pageResult = companyAccountPeriodService.getCompanyAccountPeriodPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CompanyAccountPeriodRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出公司账期信息 Excel")
    @PreAuthorize("@ss.hasPermission('tms:company-account-period:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCompanyAccountPeriodExcel(@Valid CompanyAccountPeriodPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CompanyAccountPeriodDO> list = companyAccountPeriodService.getCompanyAccountPeriodPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "公司账期信息.xls", "数据", CompanyAccountPeriodRespVO.class,
                        BeanUtils.toBean(list, CompanyAccountPeriodRespVO.class));
    }

    @GetMapping("/check-credit-alert")
    @Operation(summary = "检查授信额度预警")
    @Parameter(name = "companyId", description = "公司ID", required = true)
    @PreAuthorize("@ss.hasPermission('tms:company-account-period:query')")
    public CommonResult<Boolean> checkCreditLimitAlert(@RequestParam("companyId") Long companyId) {
        return success(companyAccountPeriodService.checkCreditLimitAlert(companyId));
    }

    @GetMapping("/check-billing-alert")
    @Operation(summary = "检查账期到期预警")
    @Parameter(name = "companyId", description = "公司ID", required = true)
    @PreAuthorize("@ss.hasPermission('tms:company-account-period:query')")
    public CommonResult<Boolean> checkBillingCycleAlert(@RequestParam("companyId") Long companyId) {
        return success(companyAccountPeriodService.checkBillingCycleAlert(companyId));
    }

}
